package com.ktjiaoyu.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjiaoyu.server.pojo.Admin;
import com.ktjiaoyu.server.pojo.PageUtil;
import com.ktjiaoyu.server.pojo.RespBean;
import com.ktjiaoyu.server.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author jieGe
 * @since 2022-01-01
 */
@Api(tags = "用于处理管理员相关的请求，对应的表为t_admin")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private IAdminService adminService;

    @Resource // 注入加密工具类的bean实例对象
    private PasswordEncoder passwordEncoder;

    /**
     * 显示管理员分页列表信息
     * @param currPageNo 当前页码
     * @param pageSize 每页显示的数据行数
     * @param name 按用户名或手机号进行模糊查询
     */
    @PreAuthorize("hasAuthority('/system/admin/list')")
    @ApiOperation("显示管理员分页列表信息")
    @GetMapping("/list")
    public RespBean getPageUserList(
            @RequestParam(value = "currPageNo", defaultValue = "1") Integer currPageNo,
            @RequestParam(value = "pageSize", defaultValue = "4")Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "sort")String sort) {
        Page<Admin> page = new Page<Admin>(currPageNo, pageSize);
        IPage<Admin> adminIPage = adminService.selectByPage(page, name,sort);
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.like("username",name);
        Integer total = adminService.count(wrapper);
        Integer current = Math.toIntExact(adminIPage.getCurrent());
        PageUtil<Admin> pageList = new PageUtil<Admin>(total,current,adminIPage.getRecords());
        return RespBean.success("获取操作员列表成功", pageList);
    }

    /**
     * 修改操作员状态, 其中enabled的值为false时表示禁用，true表示启用
     * @return
     */
    @ApiOperation("修改操作员状态")
    @PostMapping("/updateState")
    public RespBean updateState(@RequestBody Admin admin ){
            adminService.updateAdmin(admin);
            return RespBean.success("修改操作员状态成功");
    }

    /**
     * 保存操作员新增信息
     */
    @ApiOperation("保存操作员新增信息")
    @PostMapping("/saveAdmin")
    public RespBean saveAdmin(@RequestBody Admin admin) {
        // 对从前端传递过来的密码进行加密
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        // 再调用service里的saveAdmin方法实现保存新增的操作员信息的功能
        if(adminService.saveAdmin(admin)) {
            return RespBean.success("保存操作员新增信息成功");
        } else {
            return RespBean.success("保存操作员新增信息失败");
        }
    }

    @ApiOperation("通过操作员id查询操作员详情对象信息")
    @GetMapping("/getById/{id}")
    public Admin getById(@PathVariable("id")Integer id){
        return adminService.getById(id);
    }

    @ApiOperation("保存修改后操作员信息")
    @PostMapping("/updateAdmin")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if(adminService.updateAdmin(admin)>0){
            return RespBean.success("修改操作员信息成功!");
        } else {
            return RespBean.error("修改操作员信失败");
        }
    }

    @ApiOperation("删除操作员信息")
    @GetMapping("/delAdmin/{id}")
    public RespBean delAdmin(@PathVariable("id")Integer id){
        if(adminService.removeById(id)){
            return RespBean.success("删除操作员信息成功!");
        } else {
            return RespBean.error("删除操作员信息失败");
        }
    }
}

